#include "directedgraph_mt.h"

#include <directedgraph.h>
#include <node.h>

#include <QtTest/QTest>

using namespace core;
using namespace moduletest;

void DirectedGraph_mt::connect()
{
    QFETCH(bool, value);
    QFETCH(bool, result);

    QCOMPARE(value, result);
}

void DirectedGraph_mt::connect_data()
{
    DirectedGraph graph;

    Node* n1 = Node::create(0);
    Node* n2 = Node::create(0);

    graph.connect(n1, n2);

    QTest::addColumn<bool>("value");
    QTest::addColumn<bool>("result");

    QSet<Node* > conns = n2->getIncomingConnections();

    QTest::newRow("Connection made") << conns.contains(n1) << true;

    graph.connect(n2, n1);

    conns = n1->getIncomingConnections();

    QTest::newRow("Double connection made") << conns.contains(n2) << true;
}

void DirectedGraph_mt::disconnect()
{
    QFETCH(bool, value);
    QFETCH(bool, result);

    QCOMPARE(value, result);
}

void DirectedGraph_mt::disconnect_data()
{
    DirectedGraph graph;

    Node* n1 = Node::create(0);
    Node* n2 = Node::create(0);

    graph.connect(n1, n2);

    QTest::addColumn<bool>("value");
    QTest::addColumn<bool>("result");

    QSet<Node* > conns = n2->getIncomingConnections();

    QTest::newRow("Connection made") << conns.contains(n1) << true;

    graph.disconnect(n1, n2);

    QTest::newRow("Disconnect") << (n1 == 0 && n2 == 0) << true;
}

void DirectedGraph_mt::getNodes()
{
    QFETCH(bool, value);
    QFETCH(bool, result);

    QCOMPARE(value, result);
}

void DirectedGraph_mt::getNodes_data()
{
    DirectedGraph graph;

    Node* n1 = Node::create(0);
    Node* n2 = Node::create(0);
    Node* n3 = Node::create(0);
    Node* n4 = Node::create(0);

    graph.connect(n3, n1);
    graph.connect(n2, n1);
    graph.connect(n3, n2);

    const int first_size = 3;
    const int second_size = 4;
    const int third_size = 3;

    QTest::addColumn<bool>("value");
    QTest::addColumn<bool>("result");

    QSet<Node* > nodes = graph.getNodes();

    QTest::newRow("2 connections") << ((nodes.size() ==
        first_size) && nodes.contains(n1) && nodes.contains(n2) &&
        nodes.contains(n3)) << true;

    graph.connect(n3, n4);

    nodes = graph.getNodes();

    QTest::newRow("3 connections") << ((nodes.size() ==
        second_size) && nodes.contains(n1) && nodes.contains(n2) &&
        nodes.contains(n3) && nodes.contains(n4)) << true;

    graph.disconnect(n3, n4);

    nodes = graph.getNodes();

    QTest::newRow("2 connections") << ((nodes.size() ==
        third_size) && nodes.contains(n1) && nodes.contains(n2) &&
        nodes.contains(n3) && !nodes.contains(n4)) << true;
}
